<template>
  <div>
    <el-form ref="deptForm" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
          <el-option v-for="item in employees" :key="item.id" :value="item.username" :label="item.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
        <el-button size="small" @click="hCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getEmployeeSimple } from '@/api/employees'
import { addDepartments, getDepartDetail, updateDepartments } from '@/api/departments.js'
export default {
  props: {
    pid: {
      type: String,
      required: true
    },
    isedit: {
      type: Boolean,
      require: true
    },
    hhh: {
      type: Array,
      required: true
    }
  },
  data() {
    return {
      employees: [],
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur' },
          { validator: (rule, value, callback) => {
            let existednameList = this.hhh.filter(item => item.pid === this.pid).map(item => item.name)
            if (this.isedit) {
              const dept = this.hhh.find(item => item.id === this.pid)
              const pid = dept.pid
              existednameList = this.hhh.filter(item => item.pid === pid && item.id !== this.pid).map(item => item.name)
            }
            if (existednameList.includes(value)) {
              callback(new Error('名字' + value + '被占用了'))
            } else {
              callback()
            }
          }, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' },
          //  自定义校验函数
          { validator: (rule, value, callback) => {
            // console.log(this.hhh)
            let existedList = this.hhh.map(item => item.code)
            if (this.isedit) {
              existedList = this.hhh.filter(item => item.id !== this.pid).map(item => item.code)
            }
            // console.log(existedList)
            if (existedList.includes(value)) {
              callback(new Error('编号' + value + '被占用了'))
            } else {
              callback()
            }
          }, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 300, message: '部门介绍要求1-300个字符', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.loadEmployee()
    if (this.isedit) {
      this.loadDetail()
    }
  },
  methods: {
    // 关闭情况表单方法
    coslefromresct() {
      this.$refs.deptForm.resetFields()
    },
    // 获取部门详情
    async loadDetail() {
      const res = await getDepartDetail(this.pid)
      // console.log(res)
      this.form = res.data
    },
    // 获取下拉框员工方法
    async loadEmployee() {
      const rs = await getEmployeeSimple()
      // console.log('员工信息', rs)
      this.employees = rs.data
    },
    // 添加部门方法
    async doAdd() {
      const res = await addDepartments({
        name: this.form.name,
        code: this.form.code,
        manager: this.form.manager,
        introduce: this.form.introduce,
        pid: this.pid
      })
      console.log(res)
      console.log(this.pid)
      this.$emit('success')
    },
    // 编辑部门方法
    async doEait() {
      await updateDepartments(this.form)
      this.$emit('success')
    },
    // 确定按钮
    hSubmit() {
      this.$refs.deptForm.validate(valid => {
        if (valid) {
          if (this.isedit) {
            this.doEait()
          } else {
            this.doAdd()
          }
        }
      })
    },
    // 取消按钮
    hCancel() {
      this.$emit('close')
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
